{% extends "::base.html.twig" %}

{% block body %}
<div class="contentwrapper">
    <script>
        stocks = [];
        function stockSearch(stocks, id) {
            for (var i = 0; i < stocks.length; i++) {
                if (stocks[i].id == id) {
                    return i;
                }
            }
            return -1;
        }
        </script>
        <div class="heading">
            <h3>Stock Département</h3>
        </div>
        <div class="row-fluid">
            <div class="span12">
                <div id="div-list-user" class="content noPad clearfix">
                    <div id="DataTables_Table_0_wrapper" class="dataTables_wrapper" role="grid">
                        <div id="DataTables_Table_0_filter" class="dataTables_filter">
                            <table>
                                <tr>
                                {% if(is_granted('ROLE_SUPER_ADMIN')) %}
                                        <td>
                                            <label>
                                                Choisir département :
                                                <select id="select_departement">
                                            {{render(controller('GdsArticleBundle:Departement:listForStock')) }}
                                                    </select>
                                                </label>
                                            </td>
                                {% endif %}
                                            <td>
                                                <label>
                                                    Réference :
                                                    <input class="text" type="text" id="filtre_reference" aria-controls="DataTables_Table_0"/>
                                                </label>
                                            </td>
                                            <td>
                                                <label>
                                                    Désignation :
                                                    <input class="text" type="text" id="filtre_designation" aria-controls="DataTables_Table_0"/>
                                                </label>
                                            </td>
                                            <td>
                                                <label>
                                                    Code à Barre :

                                                    <input class="text" type="text" id="filtre_code_barre" aria-controls="DataTables_Table_0"/>
                                                </label>
                                            </td>
                                            <td>
                                                <label>
                                                    Quantité épuisé
                                                    <input id="quantite_epuise" type="checkbox"  />
                                                </label>
                                            </td>
                                        </tr>
                                    </table>
                                </div>
                                <div id="list_stock_article">
                        {% include 'GdsArticleBundle:Stock:allimentationList.html.twig' %}
                                    </div>
                                </div>
                            </div>
                        </div>

                        <script>
                            $('#select_departement').change(function() {
                                goToPage('{{path(pagination.route)}}?type=ajax')
                            })

                            </script>

                        </div>
                        <div class="form-actions" style="float: left;width: 98%">
                            <button class="btn btn-info" type="button" onclick="saveAllumentation()">Enregistrer</button>
                            <button class="btn" type="button" ><a href="{{path('gds_stock_departement_allimentation')}}">Annuler et Restaurer</a></button>
                        </div>
                    </div>
                    <script>
                            $('#quantite_epuise').change(function() {
                                goToPage('{{path(pagination.route)}}?type=ajax')
                            })
                            $('#filtre_reference').bind('keypress', function(e) {
                                if (e.keyCode == 13) {
                                    goToPage('{{path(pagination.route)}}?type=ajax')
                                }
                            })
                            $('#filtre_designation').bind('keypress', function(e) {
                                if (e.keyCode == 13) {
                                    goToPage('{{path(pagination.route)}}?type=ajax')
                                }
                            })
                            $('#filtre_code_barre').bind('keypress', function(e) {
                                if (e.keyCode == 13) {
                                    goToPage('{{path(pagination.route)}}?type=ajax')
                                }
                            })

                            function goToPage(url) {
                                var epuise = 0;
                                if ($('#quantite_epuise').is(':checked')) {
                                    epuise = 1;
                                }
                                $.ajax({
                                    mode: "abort",
                                    url: url,
                                    data: '&reference=' + $('#filtre_reference').val() + '&designation=' + $('#filtre_designation').val() + '&codebarre=' + $('#filtre_code_barre').val() + '&id=' + $('#select_departement').val() + '&epuise=' + epuise,
                                    success: function(data) {
                                        $('#list_stock_article').html(data);
                                    }
                                })
                            }
                            function saveAllumentation() {

                                var data = []
                                for (i = 0; i < stocks.length; i++) {
                                    if (stocks[i].new == 0){
                                        data[data.length] = stocks[i]
                                        stocks[i].new=1
                                    }
                                }
                                $.ajax({
                                    url: '{{path('gds_stock_departement_allimentation_save')}}',
                                    data: 'stocks=' + JSON.stringify(data),
                                    success: function(data) {
                                        $.msgBox({
                                            title: "Quantite",
                                            content: "Enregistrement Effectué avec succés",
                                            type: "info",
                                            buttons: [{value: "OK"}],
                                        });
                                    }
                                })
                            }
                        </script>
{% endblock body %}